package com.cn.controller.admin;

import com.cn.enums.ErrorMsg;
import com.cn.entity.IdleItem;
import com.cn.service.IdleItemService;
import com.cn.vo.Result;
import com.cn.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * 管理员闲置物品管理控制器
 * 处理闲置物品列表、更新状态、查询等功能
 */
@RestController
@RequestMapping("admin/item")
public class AdminItemController {

    @Autowired
    private IdleItemService idleItemService;

    @GetMapping("list")
    public Result idleList(HttpSession session,
                           @RequestParam("status") Integer status,
                           @RequestParam(value = "page",required = false) Integer page,
                           @RequestParam(value = "nums",required = false) Integer nums){
        // 参数校验
        if (status == null) {
            return Result.fail(ErrorMsg.PARAM_ERROR);
        }
        
        if(session.getAttribute("admin")==null){
            return Result.fail(ErrorMsg.COOKIE_ERROR);
        }
        int p=1;
        int n=8;
        if(null!=page){
            p=page>0?page:1;
        }
        if(null!=nums){
            n=nums>0?nums:8;
        }
        PageResult<IdleItem> pageResult = idleItemService.adminGetIdleList(status,p,n);
        return Result.success(pageResult);
    }

    @GetMapping("updateStatus")
    public Result updateIdleStatus(HttpSession session,
                                   @RequestParam("id") Long id,
                                   @RequestParam("status") Integer status
                                     ){
        // 参数校验
        if (id == null) {
            return Result.fail(ErrorMsg.PARAM_ERROR);
        }
        if (status == null) {
            return Result.fail(ErrorMsg.PARAM_ERROR);
        }
        
        if(session.getAttribute("admin")==null){
            return Result.fail(ErrorMsg.COOKIE_ERROR);
        }
        IdleItem idleItem =new IdleItem();
        idleItem.setId(id);
        idleItem.setIdleStatus(status.byteValue());
        if(idleItemService.updateIdleItem(idleItem)){
            return Result.success();
        }
        return Result.fail(ErrorMsg.SYSTEM_ERROR);
    }

    // 按订单闲置物品名称查询
    @GetMapping("query")
    public Result queryIdle(@RequestParam(value = "findValue",required = false) String findValue,
                            @RequestParam(value = "page",required = false) Integer page,
                            @RequestParam(value = "nums",required = false) Integer nums,
                            @RequestParam("status") Integer status){
        // 参数校验
        if (status == null) {
            return Result.fail(ErrorMsg.PARAM_ERROR);
        }
        
        if(null==findValue){
            findValue="";
        }
        int p=1;
        int n=8;
        if(null!=page){
            p=page>0?page:1;
        }
        if(null!=nums){
            n=nums>0?nums:8;
        }

        System.out.println(findValue + " " + page + " " + nums + " " + status);

        if(status == 1) {
            PageResult<IdleItem> pageResult = idleItemService.findIdleItem(findValue,p,n);
            return Result.success(pageResult);
        }
        PageResult<IdleItem> pageResult = idleItemService.findIdleItem1(findValue,status,p,n);
        return Result.success(pageResult);
    }
}